from django.test import TestCase
from django.contrib.auth.models import User
import datetime
import logging
from functions import course_reverse
from models import Course
from apps.activities.models import Activity, ActivityQuestion
from apps.news.models import Event, Announcement

class CourseViewsTestCase(TestCase):

    def setUp(self):
        logging.disable(logging.ERROR)
        self.user1 = User(username='test_user',password='sha1$0ca73$2e1468cffa8347fab31753516c88da96c3d3b44b')
        self.user1.save()
        self.course1 = Course(course_id='test_course',course_title='Test Course')
        self.course1.save()
        self.announcement1 = Announcement(title = "Test Announcement",
                                          date = datetime.datetime.now(),
                                          course = self.course1.course_id,
                                          description = 'This is a test announcement')
        self.announcement1.save()
        self.event1 = Event(title = "Test Event",
                            date = datetime.datetime.now(),
                            course = self.course1.course_id,
                            description = 'This is a test event')
        self.event1.save()
        self.event2 = Event(title = "Test Calendar Event",
                            date = datetime.datetime.now(),
                            course = self.course1.course_id,
                            description = 'This is a test event only for the calendar',
                            calendar_only = True)
        self.event2.save()
        self.activity1 = Activity(title="Test Activity", category="Tests", course=self.course1.course_id, start_date = datetime.datetime.now(), end_date=datetime.datetime.now(),is_public=True)
        self.activity1.save()
        self.question1 = ActivityQuestion(content_object=self.activity1)
        self.question1.save()
        
    def test_default_index(self):
        resp = self.client.get('')
        self.check_course_response(resp, datetime.datetime.now())
        self.assertTrue(resp.context['course'].course_id == '__default')
        self.assertTrue(len(resp.context['announcements']) == 0)
        self.assertTrue(len(resp.context['events']) == 0)

    def test_public_course_index(self):
        resp = self.client.get(course_reverse('course_index',self.course1.course_id))
        self.check_course_response(resp,datetime.datetime.now())
        self.assertTrue(resp.context['course'] == self.course1)
        self.assertTrue(len(resp.context['announcements']) == 1)
        self.assertTrue(len(resp.context['events']) == 4) #This includes the activity starting and ending as well as the 2 events
    
    def test_login_course_index(self):
        self.client.login(username='test_user',password='test_user')
        resp = self.client.get(course_reverse('course_index',self.course1.course_id))
        self.check_course_response(resp,datetime.datetime.now())
        self.assertTrue(resp.context['course'] == self.course1)
        self.assertTrue(len(resp.context['announcements']) == 1)
        self.assertTrue(len(resp.context['events']) == 4) #This includes the activity starting and ending as well as the 2 events
        
    def test_nonexistent_course(self):
        resp = self.client.get('/not_a_course/')
        self.assertEqual(resp.status_code, 404)
        
    def test_different_date(self):
        resp = self.client.get(course_reverse('course_index',self.course1.course_id),{'month':7,'year':1981})
        self.check_course_response(resp,datetime.date(1981,7,15))
        self.assertTrue(resp.context['course'] == self.course1)
    
    def test_bad_date(self):
        """Sending a bad date should just default to using today's date"""
        resp = self.client.get(course_reverse('course_index',self.course1.course_id),{'month':'goo','year':'ber'})
        self.check_course_response(resp,datetime.datetime.now())
        
    def check_course_response(self, resp, date):
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('announcements' in resp.context)
        self.assertTrue('events' in resp.context)
        self.assertTrue('year' in resp.context)
        self.assertTrue('month' in resp.context)
        self.assertTrue('permissions' in resp.context)
        self.assertTrue('course' in resp.context)
        
        self.assertTrue(resp.context['year'] == date.year)
        self.assertTrue(resp.context['month'] == date.month)
        
